#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "HuJiang";

# 将项目根目录添加到 sys.path，解决 cmd 下执行该模块找不到包的问题
import sys, os;
current_path = os.path.abspath(os.path.dirname(__file__));
separator = "\\" if os.name == "nt" else "/";
project_name = "whgc" + separator;
root_path = current_path[:current_path.find(project_name) + len(project_name)];  # 获取项目根目录
sys.path.append(root_path);
from entity.double_color_ball import *;
import numpy as bp;
import pandas as pd;
from pandas import Series, DataFrame;
import random;
from pyecharts.faker import Faker;
from pyecharts import options as opts;
from pyecharts.charts import Bar, Bar3D, Line, Line3D, Pie, Map, Geo, Funnel, Grid, Tab, Page;

'''
双色球数据处理
'''

# 双色球数据存储
def dcb_storage(dcb_list=[]):
    for item in dcb_list:
        insert_(item, "issue_no");

# 查询所有的双色球数据
def get_all_dcb():
    dcb_list = get_all_(Double_Color_Ball());
    return dcb_list;

# 构造双色球 dateframe 数据
def init_dcb_df_data(dcb_list=[]):
    column_list = ["award_date", "red_ball", "blue_ball"];
    index_list = [];
    data_list = [];
    for dcb in dcb_list:
        dcb_list = [];
        index_list.append(dcb.issue_no);
        dcb_list.append(dcb.award_date);
        dcb_list.append(dcb.red_ball);
        dcb_list.append(dcb.blue_ball);
        data_list.append(dcb_list);

    df = DataFrame(data=data_list, index=index_list, columns=column_list);
    return pd.concat([
        df["award_date"],
        df["red_ball"].str.split(" ", expand=True).rename(
            columns={0: 'red_ball1', 1: 'red_ball2', 2: 'red_ball3', 3: 'red_ball4', 4: 'red_ball5', 5: 'red_ball6'}),
        df["blue_ball"],
    ], axis=1);

# 根据dateframe数据画折线图
def draw_dcb_line_chart(df):
    df = df.sort_values(by="award_date", axis=0, ascending=True);
    x_data = df["award_date"][-20:].tolist();
    blue_data = df["blue_ball"][-20:].tolist();
    red_01_data = df["red_ball1"][-20:].tolist();

    Line().add_xaxis(
        xaxis_data=x_data
    ).add_yaxis(
        series_name="蓝球",
        y_axis=blue_data,
        itemstyle_opts=opts.ItemStyleOpts(color="#000080")
    ).add_yaxis(
        series_name="红球1",
        y_axis=red_01_data,
        itemstyle_opts=opts.ItemStyleOpts(color="#CD5C5C")
    ).set_global_opts(
        title_opts=opts.TitleOpts(title="双色球走势图", subtitle="副标题", pos_left="10%")
    ).render(
        path="D:/temp/dbc_line.html"
    )

if __name__ == '__main__':
    df = init_dcb_df_data(get_all_dcb());
    print(df);
    draw_dcb_line_chart(df);